﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibHuman;

namespace Human
{
    public partial class frmChamcong : Form
    {
        string s_mmyy = "", s_ngaylv = "", s_bophan = "", m_user = "", sql="",sql1="",m_hh="",user_mmyy="",ngay_hh="",gio_cc="",user_dn="";
        int p_thang = 0,p_nam=0,m_songay=0,n_ngaycc=0;
        AccessData mDB = new AccessData();
        DataSet ds_ngaybule = new DataSet();
        Language lan = new Language();
        DataSet ds,data_phep,data_ngaytruc,data_ngaynghibu,data_ngaynghibuhc,data_nlcuoituan;
        DataSet ds_tsan = new DataSet();
        DataSet ds_tcdi = new DataSet();
        DataSet ds_tcden = new DataSet();
        DataTable dt_lydo = new DataTable();
        bool m_admin = false,bcctc_taikhoa=false,bcc_ngaycn=false,cc_cuoituan = false;
        bool flag = false;
        public frmChamcong(string m_mmyy,string m_ngay,string m_bphan,int m_month,int m_year,bool admin,string s_userdn)
        {
            InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
            s_mmyy = m_mmyy;
            s_ngaylv = m_ngay;
            s_bophan = m_bphan;
            m_user = mDB.user;
            p_thang = m_month;
            p_nam = m_year;
            m_admin = admin;
            user_dn = s_userdn;
        }

        private void frmChamcong_Load(object sender, EventArgs e)
        {            
            DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);
            m_hh = "n" + d_ngay.Day.ToString();
            n_ngaycc = d_ngay.Day;
            ngay_hh = mDB.ngayhienhanh_server.Trim();
            gio_cc = mDB.gio_cc(s_mmyy).Trim();
            m_songay = DateTime.DaysInMonth(p_nam, p_thang);
            txtngay.Text = s_ngaylv;
            bcctc_taikhoa = mDB.bcctc_taikhoa(s_mmyy);
            bcc_ngaycn = mDB.cc_ngaycn(s_mmyy);
            load_bophan();
            load_lydonghi();      
            load_datagrid();
            ds_ngaybule = mDB.get_ngaynghile(1, s_mmyy);
            Tinh_nghiphep("", ""); 
            //
            btn_chonkp.Enabled = m_admin;
            btn_boqua.Enabled = m_admin;
            btn_xoa.Enabled = m_admin;
            btn_luu.Enabled = false;            
            ena_user(flag);           
        }
        private void ena_user(bool ena)
        {
            btn_chamcong.Enabled = ena;
            cboLydonghi.Enabled = ena;
        }
        private void load_bophan()
        {
            cboKhoa.DataSource = mDB.get_bophan().Tables[0];
            cboKhoa.ValueMember = "madv";
            cboKhoa.DisplayMember = "tenbp";
            cboKhoa.SelectedValue = s_bophan;
        }
        private void load_lydonghi()
        {
            dt_lydo = mDB.get_dmlydo_phep().Tables[0];
            DataRow r = dt_lydo.NewRow();
            r["ma"] = "X";
            r["ten"] = "";
            dt_lydo.Rows.InsertAt(r, 0);
            cboLydonghi.ValueMember = "ma";
            cboLydonghi.DisplayMember = "ten";
            cboLydonghi.DataSource = dt_lydo;
        }
        private DataSet get_nvnghits(string ngay_lv)
        {
            string sql2 = "";
            user_mmyy = m_user + s_mmyy;      
            sql2 = " select l.manv,ho||' '||ten as hoten,to_char(nts.tungay,'dd/mm/yyyy') as tungay";
            sql2 += " ,to_char(nts.denngay,'dd/mm/yyyy') as denngay";
            sql2 += " ,sn_nghiduong as songay,tl_huongpc as tile,nts.ghichu";
            sql2 += " from " + m_user + ".llnv l inner join " + m_user + ".hopdong hd on l.manv=hd.manv ";
            sql2 += " inner join " + m_user + ".max_lan ml on hd.manv=ml.manv";
            sql2 += " inner join " + m_user + ".dsnv_nghithaisan nts on l.manv=nts.manv";
            sql2 += " where hd.lan=ml.lan and hd.ngaynghi is null";
            sql2 += " and to_timestamp(to_char(nts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') = ";
            sql2 += " (select max(to_timestamp(to_char(ts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from " + m_user + ".dsnv_nghithaisan ts where nts.manv=ts.manv)";
            sql2 += " and (case nts.denngay is not null when to_timestamp('"+ ngay_lv +"','dd/mm/yyyy') between to_timestamp(to_char(nts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')";
            sql2 += " and to_timestamp(to_char(nts.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') then true else false end";
            sql2 += " or case when (nts.denngay is null and to_timestamp(to_char(nts.tungay,'dd/MM/yyyy'),'dd/MM/yyyy')";
            sql2 += " < to_timestamp('" + ngay_lv + "','dd/MM/yyyy')) then true else false end)";
            sql2 += " order by nts.manv";
            return mDB.get_data(sql2);
        }       
        private DataSet get_nvtangcuong_den(string ngay_lv)
        {
            string sql2 = "";
            user_mmyy = m_user + s_mmyy;
            sql2 = " select  0 as stt,trim(l.ho)||' '||trim(l.ten) as hoten,l.phai,c.ten as chucdanh,cc.*";
            sql2 += " from " + m_user + ".llnv l inner join " + m_user + ".hopdong hd on l.manv=hd.manv ";
            sql2 += " inner join " + m_user + ".max_lan ml on hd.manv=ml.manv";
            sql2 += " inner join " + m_user + ".nv_tangcuong_" + s_mmyy + " ntc on l.manv=ntc.manv";
            sql2 += " inner join " + m_user + ".chamcong_" + s_mmyy + " cc on l.manv=cc.manv";
            sql2 += " left join " + m_user + ".dmcd c on l.macd=c.ma";
            sql2 += " where hd.lan=ml.lan and hd.ngaynghi is null";
            sql2 += " and to_timestamp(to_char(ntc.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') = ";
            sql2 += " (select max(to_timestamp(to_char(tc.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from " + m_user + ".nv_tangcuong_" + s_mmyy + " tc where ntc.manv=tc.manv)";
            sql2 += " and (case ntc.denngay is not null when to_timestamp('" + ngay_lv + "','dd/mm/yyyy') between to_timestamp(to_char(ntc.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')";
            sql2 += " and to_timestamp(to_char(ntc.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') then true else false end";
            sql2 += " or case when (ntc.denngay is null and to_timestamp(to_char(ntc.tungay,'dd/MM/yyyy'),'dd/MM/yyyy')";
            sql2 += " < to_timestamp('" + ngay_lv + "','dd/MM/yyyy')) then true else false end)";
            sql2 += " and ntc.madv_tc = '" + s_bophan.Trim() + "' ";
            sql2 += " order by ntc.manv";
            return mDB.get_data(sql2);
        }
        private DataSet get_data()
        {
            user_mmyy = m_user + s_mmyy;            
            sql = "select  0 as stt,trim(l.ho)||' '||trim(l.ten) as hoten,l.phai,c.ten as chucdanh,d.*  ";
            sql += " from " + m_user + ".llnv l left join " + m_user + ".dmcd c on l.macd=c.ma  ";
            sql += " inner join "+m_user+".hopdong hd on l.manv=hd.manv";
            sql += " left join "+m_user+".dmcv cv on hd.macv=cv.macv";
            sql += " inner join " + m_user + ".chamcong_" + s_mmyy + " d on l.manv=d.manv ";
            sql += " inner join "+m_user+".max_lan ml on hd.manv=ml.manv";
            sql += " where d.mmyy='" + s_mmyy + "' and hd.lan=ml.lan";
            if (s_bophan != "")
                sql += " and d.madv='" + s_bophan + "'";
            if (bcctc_taikhoa)
            {
                sql += " and d.manv not in (select l.manv from "+m_user+".llnv l inner join "+m_user+".hopdong hd on l.manv=hd.manv";
                sql += " inner join "+m_user+".max_lan ml on hd.manv=ml.manv";
                sql += " inner join "+m_user+".nv_tangcuong_"+s_mmyy+" ntc on l.manv=ntc.manv";
                sql += " where hd.lan=ml.lan and hd.ngaynghi is null and to_timestamp(to_char(ntc.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') =";
                sql += " (select max(to_timestamp(to_char(tc.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from "+m_user+".nv_tangcuong_"+s_mmyy+" tc where ntc.manv=tc.manv)";
                sql += " and (case ntc.denngay is not null when to_timestamp('"+s_ngaylv+"','dd/mm/yyyy')";
                sql += " between to_timestamp(to_char(ntc.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')";
                sql += " and to_timestamp(to_char(ntc.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') then true else false end";
                sql += " or";
                sql += " case when (ntc.denngay is null and to_timestamp(to_char(ntc.tungay,'dd/MM/yyyy'),'dd/MM/yyyy') < to_timestamp('"+s_ngaylv+"','dd/MM/yyyy'))";
                sql += " then true else false end) and ntc.madv_tc <> '"+s_bophan+"'  order by ntc.manv)";
            }
            sql += " order by cv.stt,c.thutu,l.manv";
            sql1 = " select * from "+m_user+".dmlydo_phep order by stt";
            ds = mDB.get_data(sql,sql1);
            if (bcctc_taikhoa)
            {
                ds_tcden = get_nvtangcuong_den(s_ngaylv.Trim());
                ds.Merge(ds_tcden);
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                int i = 0;
                int t = mDB.get_ngaytrongthang(p_thang, p_nam);
                
                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    i++;
                    r["stt"] = i;                    
                    for (int d = 1; d <= t; d++)
                    {
                        string str = r["n" + d.ToString() + ""].ToString();                     
                        if (str.Trim() != "")
                        {
                            if (str.Trim().Length <=2)
                            {
                                DataRow[] dr = ds.Tables[1].Select("ma = '" + str.Trim() + "' ");
                                r["n" + d.ToString() + ""] = dr[0]["kihieu_phep"].ToString();
                            }
                            if (str.Trim().Length > 2)
                            {
                                string m_hh = "";
                                string[] arr = str.Split('/');
                                DataRow[] dr = ds.Tables[1].Select("ma = '" + arr[0].ToString().Trim() + "' ");
                                m_hh = dr[0]["kihieu_phep"].ToString();
                                DataRow[] dr1 = ds.Tables[1].Select("ma = '" + arr[1].ToString().Trim() + "' ");
                                m_hh +="/" + dr1[0]["kihieu_phep"].ToString();
                                r["n" + d.ToString() + ""] = m_hh.Trim(); //dr[0]["kihieu_phep"].ToString();
                            }                            
                        }
                        else
                        {
                            r["n" + d.ToString() + ""] = "";
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show(lan.Change_language_MessageText("Không có nhân viên"),AccessData.Msg,MessageBoxButtons.OK);
            }
            return ds;
        }
        private void load_datagrid()
        {
            try
            {
                string m_col = "";
                int i = 0;
                cc_cuoituan = mDB.cc_cuoituan(s_mmyy);
                get_data();
                try
                {
                    dataGridchamcong.DataSource = ds.Tables[0];
                    CurrencyManager cm = (CurrencyManager)BindingContext[dataGridchamcong.DataSource, dataGridchamcong.DataMember];
                    DataView dv = (DataView)cm.List;
                    dv.AllowNew = false;
                    dv.AllowDelete = false;
                    if (m_songay < 31)
                    {
                        for (i = m_songay + 1; i <= 31; i++)
                        {
                            m_col = "n" + i.ToString();
                            dataGridchamcong.Columns[m_col].Visible = false;
                        }
                    }
                }
                catch { }
                //dung cho benh vien cham cong ngay cuoi tuan
                DateTime d1;
                if (!cc_cuoituan && !bcc_ngaycn)
                {
                    for (i = 1; i <= m_songay; i++)
                    {
                        m_col = "n" + i.ToString();
                        d1 = new DateTime(p_nam, p_thang, i);
                        if (d1.DayOfWeek == DayOfWeek.Saturday || d1.DayOfWeek == DayOfWeek.Sunday)
                        {
                            dataGridchamcong.Columns[m_col].DefaultCellStyle.BackColor = Color.FromName("AppWorkspace"); ;
                        }
                        dataGridchamcong.Columns[m_col].ReadOnly = true;
                    }
                    DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);
                    if (d_ngay.DayOfWeek == DayOfWeek.Saturday || d_ngay.DayOfWeek == DayOfWeek.Sunday)
                        dataGridchamcong.Columns[m_hh].ReadOnly = true;
                    else
                        dataGridchamcong.Columns[m_hh].ReadOnly = false;
                }
                if(cc_cuoituan && !bcc_ngaycn)
                {
                    for (i = 1; i <= m_songay; i++)
                    {
                        m_col = "n" + i.ToString();
                        d1 = new DateTime(p_nam, p_thang, i);
                        if (d1.DayOfWeek == DayOfWeek.Sunday)
                        {
                            dataGridchamcong.Columns[m_col].DefaultCellStyle.BackColor = Color.FromName("AppWorkspace"); ;
                        }
                        dataGridchamcong.Columns[m_col].ReadOnly = true;
                    }
                    DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);
                    if (d_ngay.DayOfWeek == DayOfWeek.Sunday)
                        dataGridchamcong.Columns[m_hh].ReadOnly = true;
                    else
                        dataGridchamcong.Columns[m_hh].ReadOnly = false;
                }
                if (!cc_cuoituan && bcc_ngaycn)
                {
                    for (i = 1; i <= m_songay; i++)
                    {
                        m_col = "n" + i.ToString();
                        d1 = new DateTime(p_nam, p_thang, i);
                        if (d1.DayOfWeek == DayOfWeek.Saturday)
                        {
                            dataGridchamcong.Columns[m_col].DefaultCellStyle.BackColor = Color.FromName("AppWorkspace"); ;
                        }
                        dataGridchamcong.Columns[m_col].ReadOnly = true;
                    }
                    DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);
                    if (d_ngay.DayOfWeek == DayOfWeek.Sunday)
                        dataGridchamcong.Columns[m_hh].ReadOnly = true;
                    else
                        dataGridchamcong.Columns[m_hh].ReadOnly = false;
                }
                string s = dataGridchamcong.Rows[0].Cells[m_hh].Value.ToString().Trim();
                if (dataGridchamcong.Rows[0].Cells[m_hh].Value.ToString().Trim() != "")
                {
                    flag = false;
                }
                else
                {
                    flag = true;
                }
                txtTong.Text = ds.Tables[0].Rows.Count.ToString();
                ena_user(flag);
            }
            catch { }
        }
        private bool kiemtra_gcc()
        {
            string s = ngay_hh.Substring(0,10).Trim();
            DateTime d_giohh = Convert.ToDateTime(ngay_hh.Substring(11,5));
            DateTime d_cc = Convert.ToDateTime(gio_cc);
            DateTime d_ngaycc = Convert.ToDateTime(s_ngaylv.Trim().Substring(3,3)+s_ngaylv.Trim().Substring(0,3)+s_ngaylv.Trim().Substring(6,4));
            DateTime d_ngayhh = Convert.ToDateTime(s.Trim().Substring(3, 3) + s.Trim().Substring(0, 3) + s.Trim().Substring(6, 4));
            if (d_ngayhh == d_ngaycc)
            {
                if (d_giohh >= d_cc)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
            
        private void button1_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds.Tables[0]].Position += 1;
            this.Update();
        }
        private void cmdTiep_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds.Tables[0]].Position += 1;
            this.Update();
        }

        private void cmdTruoc_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds.Tables[0]].Position += -1;
            this.Update();
        }
        
        public void Save()
        {
            string m_value = "", s_manv = "",mmyy="";
            btn_chonkp.Enabled = m_admin;

            DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);
            mmyy = d_ngay.Month.ToString().PadLeft(2, '0') + d_ngay.Year.ToString().Substring(2,2);
            if (!cc_cuoituan)
            {
                if (d_ngay.DayOfWeek == DayOfWeek.Saturday )
                    return;
            }
            if(!bcc_ngaycn)
            {
                if (d_ngay.DayOfWeek == DayOfWeek.Sunday)
                    return;
            }
            //ds.AcceptChanges();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                m_value = dr[m_hh].ToString();
                s_manv = dr["manv"].ToString();
                if (d_ngay.DayOfWeek == DayOfWeek.Saturday)
                {
                    if (cc_cuoituan && !bcc_ngaycn)
                    {
                        if (m_value == "")
                        {
                            m_value = "X";
                            dr[m_hh] = m_value.ToString().Trim();
                        }
                    }
                }
                if (d_ngay.DayOfWeek == DayOfWeek.Saturday || d_ngay.DayOfWeek == DayOfWeek.Sunday)
                {
                    if (!cc_cuoituan && !bcc_ngaycn)
                    {
                        if (m_value == "")
                        {
                            m_value = "X";
                            dr[m_hh] = m_value.ToString().Trim();
                        }
                    }
                }
                else
                {
                    if (m_value == "")
                    {
                        m_value = "X";
                        dr[m_hh] = m_value.ToString().Trim();
                    }
                    else
                    {
                        if (m_value.Trim().Length <= 2)
                        {
                            m_value = get_malydo(m_value);
                        }
                        if (m_value.Trim().Length > 2)
                        {
                            string ma = "";
                            string[] arr = m_value.Trim().Split('/');
                            for (int k = 0; k <= arr.Rank; k++)
                            {
                                ma += get_malydo(arr[k].ToString().Trim()) +"/";
                            }
                            m_value = ma.Trim().Substring(0, ma.Trim().Length - 1);
                        }
                    }
                }       
                sql = " update " + m_user + ".chamcong_" + mmyy + " set " + m_hh + "='" + m_value + "' where manv='" + s_manv + "'  and mmyy='" + s_mmyy + "'";//and madv='" + s_bophan + "'
                try
                {
                    mDB.Execute_data(sql);
                }
                catch { }
            }            
        }
        private void btn_chamcong_Click(object sender, EventArgs e)
        {
            string m_value = "", s_manv = "", s_mmyy = "";
            btn_chonkp.Enabled = m_admin;
            DataSet dsTmp = new DataSet();
            int temp = 0;
            DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);

            ds_tsan = get_nvnghits(s_ngaylv);
            s_mmyy = d_ngay.Month.ToString().PadLeft(2, '0') + d_ngay.Year.ToString().Substring(2, 2);
            if (!cc_cuoituan && !bcc_ngaycn)
            {
                if (d_ngay.DayOfWeek == DayOfWeek.Saturday || d_ngay.DayOfWeek == DayOfWeek.Sunday)
                    return;
            }
            if (cc_cuoituan && !bcc_ngaycn)
            {
                if (d_ngay.DayOfWeek == DayOfWeek.Sunday)
                    return;
            }
            if (!cc_cuoituan && bcc_ngaycn)
            {
                if (d_ngay.DayOfWeek == DayOfWeek.Saturday)
                    return;
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    m_value = r[m_hh].ToString();
                    if (ds_tsan.Tables[0].Rows.Count > 0)
                    {
                        DataRow[] dr = ds_tsan.Tables[0].Select("manv='" + r["manv"].ToString().Trim() + "'");
                        if (dr.Length > 0)
                        {
                            if (m_value == "")
                            {
                                m_value = "TS";
                                r[m_hh] = m_value.ToString().Trim();
                            }
                        }             
                    }
                    if (ds_ngaybule.Tables[0].Rows.Count > 0)
                    {
                        DataRow[] dr1 = ds_ngaybule.Tables[0].Select("ngay='" + s_ngaylv + "' ");
                        m_value = r[m_hh].ToString();
                        s_manv = r["manv"].ToString();
                        if (dr1.Length > 0)
                        {
                            if (m_value == "")
                            {
                                m_value = "BL";
                                r[m_hh] = m_value.ToString().Trim();
                            }
                        }
                    }
                    if (d_ngay.DayOfWeek == DayOfWeek.Saturday)
                    {
                        if (cc_cuoituan && !bcc_ngaycn)
                        {
                            if (m_value == "")
                            {
                                m_value = "X";
                                r[m_hh] = m_value.ToString().Trim();
                            }
                        }
                    }
                    if(d_ngay.DayOfWeek == DayOfWeek.Saturday || d_ngay.DayOfWeek == DayOfWeek.Sunday)
                    {
                        if (!cc_cuoituan && !bcc_ngaycn)
                        {
                            if (m_value == "")
                            {
                                m_value = "X";
                                r[m_hh] = m_value.ToString().Trim();
                            }
                        }
                    }
                    else
                    {
                        if (m_value == "")
                        {
                            m_value = "X";
                            r[m_hh] = m_value.ToString().Trim();
                        }
                    }                                                          
                }
            }

            ds.AcceptChanges();
            dataGridchamcong.DataSource = ds.Tables[0];
            btn_chamcong.Enabled = false;//m_admin;
            btn_luu.Enabled = true;
        }
        #region su kien key_down
        private void txtManv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txtHoten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txtPhai_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}{F4}");
        }

        private void cboLydonghi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
            btn_chonkp.Enabled = m_admin;
        }
        private void cboKhoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }
        #endregion
        #region su kien validated
        private void cboKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == cboKhoa)
            {
                if (cboKhoa.SelectedIndex != -1)
                {
                    s_bophan = cboKhoa.SelectedValue.ToString();
                    load_datagrid();
                    cboKhoa.Enabled = false;
                }
            }
        }

        private void cboKhoa_Validated(object sender, EventArgs e)
        {
            //if (cboKhoa.SelectedIndex != -1)
            //{
            //    s_bophan = cboKhoa.SelectedValue.ToString();
            //    load_datagrid();
            //    
            //}
        }
        private void cboLydonghi_SelectedIndexChanged(object sender, EventArgs e)
        {
            string m_lydo = "";
            if (cboLydonghi.SelectedIndex != -1)
                m_lydo = cboLydonghi.SelectedValue.ToString();
            else
                m_lydo = "";
            if (mDB.StringToDate(s_ngaylv) == mDB.StringToDate(ngay_hh))
            {
                if (kiemtra_gcc())
                {
                    if (m_vlcu.Trim() != "")
                    {
                        if (m_lydo.Trim() != m_vlcu.Trim())
                        {
                            if (!m_admin)
                            {
                                if (m_vlcu.Length < 2)
                                {
                                    if (m_lydo.Trim() != "X" && m_lydo.Trim().Substring(1, 1) != "2")
                                    {
                                        MessageBox.Show("Không cho phép chọn lý do nghỉ nguyên ngày", AccessData.Msg, MessageBoxButtons.OK);
                                        cboLydonghi.Focus();
                                        return;
                                    }
                                }
                                if (m_vlcu.Length == 2)
                                {
                                    if (m_vlcu.Trim().Substring(1, 1) == "2")
                                    {
                                        if (m_lydo.Length == 2)
                                        {
                                            if (m_lydo.Trim().Substring(1, 1) != "2")
                                            {
                                                MessageBox.Show("Không cho phép chọn lý do nghỉ nguyên ngày", AccessData.Msg, MessageBoxButtons.OK);
                                                cboLydonghi.Focus();
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //if (m_vlcu.Trim() != "")
            //{
            //    if (m_lydo.Trim() != m_vlcu.Trim())
            //    {
            //        if (!m_admin)
            //        {
            //            if (m_vlcu.Length < 2)
            //            {                           
            //                if (m_lydo.Trim()!="X" && m_lydo.Trim().Substring(1, 1) != "2")
            //                {
            //                    MessageBox.Show("Không cho phép chọn lý do nghỉ nguyên ngày",AccessData.Msg,MessageBoxButtons.OK);
            //                    cboLydonghi.Focus();
            //                    return;
            //                }
            //            }
            //            if (m_vlcu.Length == 2)
            //            {
            //                if (m_vlcu.Trim().Substring(1, 1) == "2")
            //                {
            //                    if (m_lydo.Length == 2)
            //                    {
            //                        if (m_lydo.Trim().Substring(1, 1) != "2")
            //                        {
            //                            MessageBox.Show("Không cho phép chọn lý do nghỉ nguyên ngày", AccessData.Msg, MessageBoxButtons.OK);
            //                            cboLydonghi.Focus();
            //                            return;
            //                        }
            //                    }
            //                }
            //            }
            //        }                    
            //    }
            //}
        }   
        private void cboLydonghi_Validated(object sender, EventArgs e)
        {
            string ma_nv = "", m_lydo = "";
            decimal phepcon = 0, ts_ngaytruc = 0, ts_ngaynghibu = 0,ts_nlcuoituan=0,ts_ngaynghibuhc=0;
            ma_nv = txtManv.Text;
            if (cboLydonghi.SelectedIndex != -1)
            {
                m_lydo = cboLydonghi.SelectedValue.ToString();
            }
            else
                m_lydo = "";
            if (m_lydo.Trim() == "NB" || m_lydo.Trim() == "B2" || m_lydo.Trim() == "BU" || m_lydo.Trim() == "U2" || m_lydo.Trim() == "PN" || m_lydo.Trim() == "P2")
            {
                data_phep = mDB.get_nghiphep(p_nam.ToString().Trim(), s_mmyy);
                data_ngaynghibu = mDB.get_ngaynghibu(s_mmyy);
                data_ngaynghibuhc = mDB.get_ngaynghibuhc(s_mmyy);
                data_ngaytruc = mDB.get_ngaytruc(s_mmyy, n_ngaycc);
                data_nlcuoituan = mDB.get_nlcuoituan(s_mmyy, p_thang.ToString(), p_nam.ToString(), n_ngaycc);
            }
            if (m_lydo == "KT" || m_lydo == "TS" || m_lydo == "S2" || m_lydo == "M2")
            {
                if (txtPhai.Text == "Nam")
                {
                    MessageBox.Show(lan.Change_language_MessageText("Chọn lý do không hợp lệ!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    cboLydonghi.Focus();
                    return;
                }
            }
            if (m_lydo == "P2")
            {
                try
                {
                    DataRow[] r = data_phep.Tables[0].Select("manv='" + ma_nv + "'");
                    if (r.Length > 0)
                    {
                        phepcon = Convert.ToDecimal(r[0]["phep_con"].ToString());
                        if (phepcon <= 0)
                        {
                            MessageBox.Show(lan.Change_language_MessageText("Nhân viên mã số \"") + ma_nv + lan.Change_language_MessageText("\" đã hết ngày phép!"));
                            return;
                        }
                    }
                }
                catch { return; }
            }
            if (m_lydo == "PN")
            {
                try
                {
                    DataRow[] r = data_phep.Tables[0].Select("manv='" + ma_nv + "'");
                    if (r.Length > 0)
                    {
                        phepcon = Convert.ToDecimal(r[0]["phep_con"].ToString());
                        if (phepcon < 1)
                        {
                            MessageBox.Show(lan.Change_language_MessageText("Nhân viên mã số \"") + ma_nv + lan.Change_language_MessageText("\" đã hết ngày phép!"));
                            return;
                        }
                    }
                }
                catch { return; }
            }
            
            if (m_lydo == "B2")
            {
                DataRow[] r = data_ngaytruc.Tables[0].Select("manv='" + ma_nv + "'");
                DataRow[] r1 = data_ngaynghibu.Tables[0].Select("manv='" + ma_nv + "'");
                if (r.Length > 0)
                {
                    ts_ngaytruc = decimal.Parse(r[0]["ts_ngaytruc"].ToString());
                    ts_ngaynghibu = decimal.Parse(r1[0]["ts_ngaynghibu"].ToString());
                    if ((ts_ngaytruc - ts_ngaynghibu) < decimal.Parse("0.5"))
                    {
                        MessageBox.Show(lan.Change_language_MessageText("Số ngày nghỉ bù lớn hơn số ngày trực"),AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            if (m_lydo == "NB")
            {
                DataRow[] r = data_ngaytruc.Tables[0].Select("manv='" + ma_nv + "'");
                DataRow[] r1 = data_ngaynghibu.Tables[0].Select("manv='" + ma_nv + "'");
                if (r.Length > 0)
                {
                    ts_ngaytruc = decimal.Parse(r[0]["ts_ngaytruc"].ToString());
                    ts_ngaynghibu = decimal.Parse(r1[0]["ts_ngaynghibu"].ToString());
                    if ((ts_ngaytruc - ts_ngaynghibu) < 1)
                    {
                        MessageBox.Show(lan.Change_language_MessageText("Số ngày nghỉ bù lớn hơn số ngày trực"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            if (m_lydo == "U2")
            {
                DataRow[] hr = data_nlcuoituan.Tables[0].Select("manv='" + ma_nv + "'");
                DataRow[] hr1 = data_ngaynghibuhc.Tables[0].Select("manv='" + ma_nv + "'");
                if (hr.Length > 0)
                {
                    ts_nlcuoituan = decimal.Parse(hr[0]["ts_nlcuoituan"].ToString());
                    ts_ngaynghibuhc = decimal.Parse(hr1[0]["ts_ngaynghibuhc"].ToString());
                    if ((ts_nlcuoituan - ts_ngaynghibuhc)< decimal.Parse("0.5"))
                    {
                        MessageBox.Show(lan.Change_language_MessageText("Số ngày nghỉ bù lớn hơn số ngày làm cuối tuần"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            if (m_lydo == "BU")
            {
                DataRow[] hr = data_nlcuoituan.Tables[0].Select("manv='" + ma_nv + "'");
                DataRow[] hr1 = data_ngaynghibuhc.Tables[0].Select("manv='" + ma_nv + "'");
                if (hr.Length > 0)
                {
                    ts_nlcuoituan = decimal.Parse(hr[0]["ts_nlcuoituan"].ToString());
                    ts_ngaynghibuhc = decimal.Parse(hr1[0]["ts_ngaynghibuhc"].ToString());
                    if ((ts_nlcuoituan - ts_ngaynghibuhc) < 1)
                    {
                        MessageBox.Show(lan.Change_language_MessageText("Số ngày nghỉ bù lớn hơn số ngày làm cuối tuần"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            if (mDB.StringToDate(s_ngaylv) == mDB.StringToDate(ngay_hh))
            {
                if (kiemtra_gcc())
                {
                    if (m_vlcu.Trim() != "")
                    {
                        if (m_lydo.Trim() != m_vlcu.Trim())
                        {
                            if (m_admin)
                            {
                                if (m_vlcu.Length < 2)
                                {
                                    if (m_lydo.Trim() == "X")
                                    {
                                        m_lydo = m_vlcu;
                                    }
                                    if (m_lydo.Length == 2)
                                    {
                                        if (m_lydo.Trim().Substring(1, 1) == "2")
                                        {
                                            m_lydo = "X2/" + m_lydo;
                                        }                                        
                                    }
                                }
                                if (m_vlcu.Length == 2)
                                {
                                    if (m_vlcu.Trim().Substring(1, 1) != "2")
                                    {
                                        if (m_lydo.Length == 2)
                                        {
                                            if (m_lydo.Trim().Substring(1, 1) == "2")
                                            {
                                                m_lydo = "X2/" + m_lydo;
                                            }                                            
                                        }
                                    }
                                    if (m_vlcu.Trim().Substring(1, 1) == "2")
                                    {
                                        if (m_lydo.Length == 2)
                                        {
                                            if (m_lydo.Trim().Substring(1, 1) == "2")
                                            {
                                                string[] arr = m_vlcu.Trim().Split('/');
                                                m_lydo = arr[0].ToString().Trim() + "/" + m_lydo;
                                            }                                            
                                        }
                                    }

                                }
                                if (m_vlcu.Length > 2)
                                {
                                    if (m_lydo.Length == 2)
                                    {
                                        if (m_lydo.Trim().Substring(1, 1) == "2")
                                        {
                                            string[] arr = m_vlcu.Split('/');
                                            m_lydo = arr[0].ToString().Trim() + "/" + m_lydo;
                                        }                                        
                                    }
                                }
                            }
                            else
                            {
                                if (m_vlcu.Length < 2)
                                {
                                    if (m_lydo.Trim() == "X")
                                    {
                                        m_lydo = m_vlcu;
                                    }
                                    if (m_lydo.Length == 2)
                                    {
                                        if (m_lydo.Trim().Substring(1, 1) == "2")
                                        {
                                            m_lydo = "X2/" + m_lydo;
                                        }
                                    }
                                }
                                if (m_vlcu.Length == 2)
                                {
                                    if (m_vlcu.Trim().Substring(1, 1) == "2")
                                    {
                                        if (m_lydo.Length == 2)
                                        {
                                            if (m_lydo.Trim().Substring(1, 1) == "2")
                                            {
                                                string[] arr = m_vlcu.Trim().Split('/');
                                                m_lydo = arr[0].ToString().Trim() + "/" + m_lydo;
                                            }
                                        }
                                    }

                                }
                            }
                        }
                    }
                } 
            }
            if (mDB.StringToDate(s_ngaylv) < mDB.StringToDate(ngay_hh))
            {
                if (m_vlcu.Trim() != "")
                {
                    if (m_lydo.Trim() != m_vlcu.Trim())
                    {
                        if (m_admin)
                        {
                            if (m_vlcu.Length < 2)
                            {
                                if (m_lydo.Trim() == "X")
                                {
                                    m_lydo = m_vlcu;
                                }
                                if (m_lydo.Length == 2)
                                {
                                    if (m_lydo.Trim().Substring(1, 1) == "2")
                                    {
                                        m_lydo = "X2/" + m_lydo;
                                    }                                    
                                }
                            }
                            if (m_vlcu.Length == 2)
                            {
                                if (m_vlcu.Trim().Substring(1, 1) != "2")
                                {
                                    if (m_lydo.Length == 2)
                                    {
                                        if (m_lydo.Trim().Substring(1, 1) == "2")
                                        {
                                            m_lydo = "X2/" + m_lydo;
                                        }                                        
                                    }
                                }
                                if (m_vlcu.Trim().Substring(1, 1) == "2")
                                {
                                    if (m_lydo.Length == 2)
                                    {
                                        if (m_lydo.Trim().Substring(1, 1) == "2")
                                        {
                                            string[] arr = m_vlcu.Trim().Split('/');
                                            m_lydo = arr[0].ToString().Trim() + "/" + m_lydo;
                                        }                                        
                                    }
                                }

                            }
                            if (m_vlcu.Length > 2)
                            {
                                if (m_lydo.Length == 2)
                                {
                                    if (m_lydo.Trim().Substring(1, 1) == "2")
                                    {
                                        string[] arr = m_vlcu.Split('/');
                                        m_lydo = arr[0].ToString().Trim() + "/" + m_lydo;
                                    }                                    
                                }
                            }
                        }
                        else
                        {
                            if (m_vlcu.Length < 2)
                            {
                                if (m_lydo.Trim() == "X")
                                {
                                    m_lydo = m_vlcu;
                                }
                                if (m_lydo.Length == 2)
                                {
                                    if (m_lydo.Trim().Substring(1, 1) == "2")
                                    {
                                        m_lydo = "X2/" + m_lydo;
                                    }
                                }
                            }
                            if (m_vlcu.Length == 2)
                            {
                                if (m_vlcu.Trim().Substring(1, 1) == "2")
                                {
                                    if (m_lydo.Length == 2)
                                    {
                                        if (m_lydo.Trim().Substring(1, 1) == "2")
                                        {
                                            string[] arr = m_vlcu.Trim().Split('/');
                                            m_lydo = arr[0].ToString().Trim() + "/" + m_lydo;
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow[] dr = ds.Tables[0].Select("manv='" + ma_nv + "'");
                if (dr.Length > 0)
                {
                    if (m_lydo.Trim() != "")
                    {
                        if (m_lydo.Trim().Length <= 2)
                        {
                            m_lydo = get_kyhieuphep(m_lydo);
                        }
                        if (m_lydo.Trim().Length > 2)
                        {
                            string kyhieu = "";
                            string[] arr = m_lydo.Split('/');
                            for (int k = 0; k <= arr.Rank; k++)
                            {
                                kyhieu += get_kyhieuphep(arr[k].ToString().Trim())+"/";
                            }
                            m_lydo = kyhieu.Trim().Substring(0, kyhieu.Trim().Length - 1);
                        }
                    }
                    dr[0][m_hh] = m_lydo;
                }
            }
            ds.AcceptChanges();
            dataGridchamcong.DataSource = ds.Tables[0];
            btn_luu.Enabled = true;
        }
        #endregion

        private DataSet Tinh_nghiphep(string thangnam, string manv)
        {
            int m_songay = 0;
            m_songay = 31;
            string n = "",s_pn="",sql="";
            for (int i = 1; i <= m_songay; i++)
            {
                n = "n" + i.ToString();
                s_pn += " case when trim(upper(" + n + ")) ='PN' then 1 when trim(upper(" + n + ")) ='P2' then 0.5 else 0 end +";
            }
            s_pn = s_pn.Substring(0, s_pn.Length - 1) + " as tongpn";
            try
            {
                sql = "select " + s_pn + " from " + m_user + ".chamcong_" + s_mmyy + " where mmyy='" + thangnam + "' and manv='" + manv + "'";
                return mDB.get_data(sql); 
            }
            catch { return null; }
        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            Save();
            load_datagrid();
            ena_user(false);//m_admin
            btn_luu.Enabled = false;
        }

        private void btn_boqua_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                string m_value = "", s_manv = "", mmyy = "";
                btn_chonkp.Enabled = m_admin;
                DataSet dsTmp = new DataSet();
                DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);
                mmyy = d_ngay.Month.ToString().PadLeft(2, '0') + d_ngay.Year.ToString().Substring(2, 2);
                m_value = "";
                s_manv = r["manv"].ToString().Trim();
                sql = "update "+m_user+".chamcong_" + mmyy + " set " + m_hh + "='" + m_value + "' ";
                sql += " where manv='" + s_manv + "'  and mmyy='" + mmyy + "'";//and madv='" + s_bophan + "'
                try
                {
                    mDB.Execute_data(sql);
                }
                catch { }
            }
            load_datagrid();
            btn_boqua.Enabled = false;// m_admin;
            btn_xoa.Enabled = false;
            btn_luu.Enabled = false; //m_admin;
            btn_chamcong.Enabled = false;//m_admin;
        }
        private void btn_xoa_Click(object sender, EventArgs e)
        {
            string m_value = "", s_manv = "", mmyy = "";
            btn_chonkp.Enabled = m_admin;
            DataSet dsTmp = new DataSet();
            DateTime d_ngay = mDB.StringToDateTime(s_ngaylv);
            mmyy = d_ngay.Month.ToString().PadLeft(2, '0') + d_ngay.Year.ToString().Substring(2, 2);
            m_value = "";
            s_manv = txtManv.Text.Trim();
            sql = "update " + m_user + ".chamcong_" + mmyy + " set " + m_hh + "='" + m_value + "' ";
            sql += " where manv='" + s_manv + "'  and mmyy='" + mmyy + "'";//and madv='" + s_bophan + "'
            try
            {
                mDB.Execute_data(sql);
            }
            catch { }
            load_datagrid();
            btn_boqua.Enabled = false;// m_admin;
            btn_xoa.Enabled = false;
            btn_luu.Enabled = false; //m_admin;
            btn_chamcong.Enabled = false;//m_admin;
        }    
        private void btn_chonkp_Click(object sender, EventArgs e)
        {
            cboKhoa.Enabled = true;
            SendKeys.Send("{F4}");
            cboKhoa.Focus();
        }

        private void btn_ketthuc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmChamcong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.KeyCode == Keys.L)
            {
                btn_luu_Click(null, null);
            }
            if (e.Alt && e.KeyCode == Keys.C)
            {
                btn_chamcong_Click(null, null);
            }
            if (e.Alt && e.KeyCode == Keys.B)
            {
                btn_boqua_Click(null, null);
            }
            if (e.Alt && e.KeyCode == Keys.K)
            {
                btn_chonkp_Click(null, null);
            }
            if (e.Alt && e.KeyCode == Keys.T)
            {
                btn_ketthuc_Click(null, null);
            }
        }
        
        private void dataGridchamcong_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridchamcong_CellContentClick(null, null);
        }

        private void dataGridchamcong_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dataGridchamcong.Rows[e.RowIndex].ErrorText = String.Empty;
        }

        private void dataGridchamcong_CurrentCellChanged(object sender, EventArgs e)
        {
            dataGridchamcong_CellContentClick(null, null);
        }

        private void dataGridchamcong_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
           
        }
        // lay thong tin malydo cho truong hop hien thi ky hieu
        private string get_malydo(string kyhieu)
        {
            string ma = "";
            if (kyhieu.Trim() != "")
            {
                DataRow[] dr = dt_lydo.Select("kihieu_phep = '" + kyhieu.Trim() + "'");
                ma= dr[0]["ma"].ToString().Trim();
            }
            return ma;
        }
        private string get_kyhieuphep(string ma)
        {
            string kyhieu = "";
            if (ma.Trim() != "")
            {
                DataRow[] dr = dt_lydo.Select("ma = '" + ma.Trim() + "'");
                kyhieu= dr[0]["kihieu_phep"].ToString().Trim();
            }
            return kyhieu;
        }
        string m_vlcu = "";
        private void dataGridchamcong_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataRowView arv = (DataRowView)(dataGridchamcong.CurrentRow.DataBoundItem);
                txtManv.Text = arv["manv"].ToString();
                txtHoten.Text = arv["hoten"].ToString();
                txtPhai.Text = arv["phai"].ToString();
                m_vlcu = arv[m_hh].ToString();
                if (m_vlcu.Trim().Length <= 2)
                {
                    m_vlcu = get_malydo(m_vlcu.Trim());
                }
                else
                {
                    string ma = "";
                    string []arr = m_vlcu.Trim().Split('/');
                    for (int k = 0; k <= arr.Rank; k++)
                    {
                        ma += get_malydo(arr[k].ToString().Trim())+"/";
                    }
                    m_vlcu = ma.Trim().Substring(0, ma.Trim().Length - 1);
                }
                if (m_vlcu.Length <= 2)
                {
                    if (m_vlcu.Length < 2)
                    {
                        if (m_vlcu.Trim() != "" && m_vlcu.Trim() != "X" )
                        {
                            cboLydonghi.SelectedValue = m_vlcu.Trim();
                            if (!m_admin)
                            {
                                if (!flag)
                                {
                                    cboLydonghi.Enabled = m_admin;
                                    btn_luu.Enabled = m_admin;
                                    btn_xoa.Enabled = m_admin;
                                }
                                else
                                {
                                    cboLydonghi.Enabled = flag;
                                    btn_luu.Enabled = flag;
                                    btn_xoa.Enabled = flag;
                                }
                            }
                            else
                            {
                                cboLydonghi.Enabled = m_admin;
                                btn_luu.Enabled = m_admin;
                                btn_xoa.Enabled = m_admin;
                            }
                        }
                        else
                        {
                            cboLydonghi.SelectedValue = m_vlcu.Trim();
                            if (!m_admin)
                            {
                                if (!flag)
                                {
                                    if (kiemtra_gcc())
                                    {
                                        cboLydonghi.Enabled = true;
                                        btn_luu.Enabled = true;
                                        btn_xoa.Enabled = false;
                                    }
                                    else
                                    {
                                        cboLydonghi.Enabled = m_admin;
                                        btn_luu.Enabled = m_admin;
                                        btn_xoa.Enabled = false;
                                    }
                                }
                                else
                                {
                                    cboLydonghi.Enabled = flag;
                                    btn_luu.Enabled = flag;
                                    btn_xoa.Enabled = false;
                                }
                            }
                            else
                            {
                                cboLydonghi.Enabled = m_admin;
                                btn_luu.Enabled = m_admin;
                                btn_xoa.Enabled = m_admin;
                            }
                            SendKeys.Send("{F4}");
                        }
                    }
                    if (m_vlcu.Length == 2)
                    {
                        if (m_vlcu.Trim() != "" && m_vlcu.Trim().Trim().Substring(1,1) != "2")
                        {
                            cboLydonghi.SelectedValue = m_vlcu.Trim();
                            if (!m_admin)
                            {
                                if (!flag)
                                {
                                    cboLydonghi.Enabled = m_admin;
                                    btn_luu.Enabled = m_admin;
                                    btn_xoa.Enabled = m_admin;
                                }
                                else
                                {
                                    cboLydonghi.Enabled = flag;
                                    btn_luu.Enabled = flag;
                                    btn_xoa.Enabled = flag;
                                }
                            }
                            else
                            {
                                cboLydonghi.Enabled = m_admin;
                                btn_luu.Enabled = m_admin;
                                btn_xoa.Enabled = m_admin;
                            }
                        }
                        else
                        {
                            cboLydonghi.SelectedValue = m_vlcu.Trim();
                            if (!m_admin)
                            {
                                if (!flag)
                                {
                                    if (kiemtra_gcc())
                                    {
                                        cboLydonghi.Enabled = true;
                                        btn_luu.Enabled = true;
                                        btn_xoa.Enabled = false;
                                    }
                                    else
                                    {
                                        cboLydonghi.Enabled = m_admin;
                                        btn_luu.Enabled = m_admin;
                                        btn_xoa.Enabled = false;
                                    }
                                }
                                else
                                {
                                    cboLydonghi.Enabled = flag;
                                    btn_luu.Enabled = flag;
                                    btn_xoa.Enabled = false;
                                }
                            }
                            else
                            {
                                cboLydonghi.Enabled = m_admin;
                                btn_luu.Enabled = m_admin;
                                btn_xoa.Enabled = m_admin;
                            }
                            SendKeys.Send("{F4}");
                        }
                    }                                       
                }
                else
                {                    
                    string[] arr = m_vlcu.Split('/');
                    cboLydonghi.SelectedValue = arr[1].ToString().Trim();
                    cboLydonghi.Enabled = m_admin;
                    btn_luu.Enabled = m_admin;
                    btn_xoa.Enabled = m_admin;
                    SendKeys.Send("{F4}");
                }                
            }
            catch
            {
            }
        }               
    }
}